package com.rlz.base.util;

import java.io.Serializable;

/**
 * Created by RLZ
 * on 2020/10/30
 * <p>
 * 一个 分页加载的帮助 类
 */
public class Paging implements Serializable {


    public int pageSize;//每页的数据大小

    public int totalNumber;//数据总条数

    public int pageCount;

    public int curPage;//当前页面

    public String type; // 类型

    //public int totalPage;


    public Paging() {
        this.pageSize = 10;
        this.totalNumber = 1;
        this.curPage = 1;
        this.pageCount = 1;
    }

    public Paging(String type) {
        this.type = type;
        this.pageSize = 10;
        this.totalNumber = 1;
        this.pageCount = 1;
        this.curPage = 1;
    }

    public void reset() {
        this.pageSize = 10;
        this.totalNumber = 1;
        this.curPage = 1;
        this.pageCount = 1;
    }

    public void success(int recordCnt) {
        this.totalNumber = recordCnt;
        this.pageCount = getPageCount(recordCnt, this.pageSize);
    }

    public void setTotalPage(int totalPage) {
        this.pageCount = totalPage;
    }

    public void error() {
        this.curPage = this.curPage > 1 ? this.curPage - 1 : 1;
    }

    /**
     * 计算页数
     */
    private int getPageCount(int totalNum, int pageSize) {
        if (totalNum == 0 || pageSize == 0)
            return totalNum;

        int pageCount = totalNum / pageSize;
        if (totalNum % pageSize > 0) {
            pageCount += 1;
        }
        return pageCount;
    }
}
